<?php
/**
 * Created by PhpStorm.
 * User: OnlyOne
 * Date: 2018/7/10
 * Time: 18:51
 */

$filePath = "";
if (1) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

//        if (file_exists("Classes/" . $_FILES["file"]["name"])) {
//            echo $_FILES["file"]["name"] . " already exists. ";
//        } else {
        move_uploaded_file($_FILES["file"]["tmp_name"],
            "Classes/" . $_FILES["file"]["name"]);
        $filePath = "Classes/" . $_FILES["file"]["name"];
        echo "Stored in: " . "Classes/" . $_FILES["file"]["name"];
        ImportData($filePath);
//        }
    }
} else {
    echo "Invalid file";
}


function UploadFile()
{

}

function ImportData($filePath)
{
    include_once('./Classes/PHPExcel.php');

    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
//date_default_timezone_set('Europe/London');
    $data = [];

    $PHPExcel = new PHPExcel();
    $filename = $filePath;
//如果excel文件后缀名为.xls，导入这个类
    $exts = substr($filename, (strrpos($filename, ".") + 1));
    if ($exts == 'xls') {
        include_once('./Classes/PHPExcel/Reader/Excel5.php');
        $PHPReader = new \PHPExcel_Reader_Excel5();
    } else if ($exts == 'xlsx') {
        include_once('./Classes/PHPExcel/Reader/Excel2007.php');
        $PHPReader = new \PHPExcel_Reader_Excel2007();
    }
//载入文件
    $PHPExcel = $PHPReader->load($filename);
//获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推

    $currentSheet = $PHPExcel->getSheet(0);
//获取总列数
    $allColumn = $currentSheet->getHighestColumn();
//获取总行数
    $allRow = $currentSheet->getHighestRow();
//循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
    for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
        //从哪列开始，A表示第一列
        for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
            //数据坐标
            $address = $currentColumn . $currentRow;
            //读取到的数据，保存到数组$arr中
            $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
        }
    }
    print_r($data);
}
